﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace VisionRoc.Tool
{
    public class CsvAPI
    {
        public static void ReadCsv(string path, out List<string> data)
        {
            StreamReader sr;
            data = new List<string>();
            try
            {
                using (sr = new StreamReader(path, Encoding.GetEncoding("GB2312")))
                {
                    string str = "";
                    while ((str = sr.ReadLine()) != null)
                    {
                        data.Add(str);
                    }
                }
            }
            catch (Exception ex)
            {
                foreach (Process process in Process.GetProcesses())
                {
                    if (process.ProcessName.ToUpper().Equals("EXCEL"))
                        process.Kill();
                }
                GC.Collect();
                Thread.Sleep(10);
                Console.WriteLine(ex.StackTrace);
                using (sr = new StreamReader(path, Encoding.GetEncoding("GB2312")))
                {
                    string str = "";
                    while ((str = sr.ReadLine()) != null)
                    {
                        data.Add(str);
                    }
                }
            }
        }


        /// <summary>
        /// 写入CSV文件 
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="CD">相机</param>
        public static async void WriteDataCsv(string path, double PE_Dis, double NE_Dis, double DpeNe_Dis, bool vision)
        {
            await Task.Run(() =>
            {
                try
                {
                    string m_path = path + "\\" + "RocCsv" + "\\" + string.Format("{0:Y}", DateTime.Now) + "\\";
                    if (!Directory.Exists(m_path))
                    {
                        Directory.CreateDirectory(m_path);
                    }
                    path = m_path + "\\" + Tool.Time() + ".csv";
                    if (!System.IO.File.Exists(path))   //文件不存在时，创建新文件，并写入文件标题
                    {
                        using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
                        {
                            using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                            {
                                StringBuilder sb = new StringBuilder();
                                sb.Append("时间").Append(",").Append("正极到隔膜距离").Append(",").Append("负极到隔膜距离").Append(",").Append("正极到负极距离").Append(",").Append("4号区域二维码信息").Append("视觉判定结果");
                                //把标题内容写入到文件流中
                                sw.WriteLine(sb);
                                sw.Flush();
                                sw.Close();
                                sw.Dispose();
                                fs.Close();
                                fs.Dispose();

                            }
                        }
                    }
                    using (StreamWriter msw = new StreamWriter(path, true, Encoding.Default))
                    {
                        StringBuilder msb = new StringBuilder();
                        msb.Append(DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss")).Append(",").Append(PE_Dis).Append(",").Append(NE_Dis).Append(",").Append(DpeNe_Dis).Append(",").Append(vision);
                        //把数据内容写入文件中
                        msw.WriteLine(msb);
                        msw.Flush();
                        msw.Close();
                        msw.Dispose();
                    }
                }
                catch
                {

                }
            });
        }

    }


}
